
package jp.ize.iwp.avm1.events;

class EventDispatcher
{
	// {{{ properties

	public var listeners:Hash<Array<Dynamic>>;

	// }}}
	// {{{ public function new() : Void

	public function new() : Void
	{
		listeners = new Hash();
	}

	// }}}
	// {{{ public function dispatchEvent( e:Dynamic ) : Void

	public function dispatchEvent( e:Dynamic ) : Void
	{
		if ( listeners.exists(e.type) ) {
			var funcs = listeners.get(e.type);
			for ( func in funcs ) {
				func(e);
			}
		}
	}

	// }}}
	// {{{ public function addEventListener( event:String, func:Dynamic )

	public function addEventListener( event:String, func:Dynamic )
	{
		//Reflect.setField(graphics,event,func);
		if ( listeners.exists(event) ) {
			var ary = listeners.get(event);
			ary.push( func );
		} else {
			var ary = new Array<Dynamic>();
			ary.push ( func );
			listeners.set( event, ary );
		}
	}

	// }}}
	// {{{ public function removeEventListener( event:String, ?func:Dynamic )
	
	public function removeEventListener( event:String, ?func:Dynamic )
	{
		if ( listeners.exists(event) ) {
			var ary = listeners.get(event);
			if ( func==null ) {
				listeners.remove(event);
			} else {
				ary.remove( func );
			}
		}
	}

	// }}}
}
